Thread: [Warning] Incompatible pointer type

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    [Warning] Incompatible pointer type

    My function is defined like this in a header:
    Code:
    typedef int(*TFComp)(const void *, const void *);
    And I define it like this in a particular program:

    Code:
    int cmp(const Ax m, const Ax n)
    {
      ...
    }
    I use it in another function, defined like this:
    Code:
    void *Locate(..., TFComp cmp)
    Ax is a pointer to a struct.

    I get this warning message from the compiler:
    [Warning] passing arg 5 of `Locate' from incompatible pointer type

    It runs fine, the function does it's job, but I don't understand what that warning means.
    Last edited by dgs012; 02-20-2011 at 10:59 AM. Reason: Title mistake

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Because const Ax and const void* are different types. duh~


    Code:
    int cmp(const Ax m, const Ax n)
    {
      ...
    }
    change to


    Code:
    int cmp(const void *a, const void *b)
    {
      const Ax m = a;
      const Ax n = b;
      ...
    }

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    Thanks, but that wasn't it.

    I just needed to pass the cmp function to Locate as &cmp...

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I don't think so.
    Edit: p = &cmp; and p = cmp; are exactly the same in C.
    Last edited by Bayint Naung; 02-20-2011 at 11:22 AM.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Because const Ax is not the same thing as const void*. It may seem like it should work because of how void* is a generic pointer, but it is wrong.

    The right fix is to make cmp() take two const void* arguments. You can convert them to your Ax type in the function.

    The reason this must be done is because the compiler, when it calls your function, does not know what its prototype looks like. What it knows is that it's calling a function that takes two void* arguments. void* works as a generic pointer only when the compiler knows both types. Much like with qsort(), the compiler only knows that it's calling a function (through a pointer) that takes two void* arguments.

    It generally works because most systems have pointers that are all the same size and representation. But assume that there is a system whose void* carries around extra information, making it, say, 128 bits while other pointers are 32 bits. Inside of Locate() you have something like this:
    Code:
    cmp(arg1, arg2);
    The compiler sees that cmp is a pointer to a function that takes two void* arguments. It dutifully converts arg1 and arg1 to void*, passing each as 16 bytes. But your function takes two "normal" pointers, each being 4 bytes. There's no way your function can know that it was passed two 16-byte arguments, so it will get everything all wrong.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    That fixed it, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  2. Passing Arguments
    By bolivartech in forum C Programming
    Replies: 13
    Last Post: 10-15-2009, 01:31 PM
  3. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM